package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.WorkflowApplyLineUserRpcService;
import com.hunttown.mes.rpc.domain.WorkflowApplyLineUserDTO;
import com.hunttown.mes.rpc.domain.query.WorkflowApplyLineUserDTOQuery;
import com.hunttown.mes.service.WorkflowApplyLineUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.WorkflowApplyLineUserConverts.convert;

/**
 * created by wangjunfu on 2020-12-24 09:18:14
 */
@Service
public class WorkflowApplyLineUserRpcServiceImpl implements WorkflowApplyLineUserRpcService {

    private final Logger logger = LoggerFactory.getLogger(WorkflowApplyLineUserRpcServiceImpl.class);

    private final WorkflowApplyLineUserService workflowApplyLineUserService;

    @Autowired
    public WorkflowApplyLineUserRpcServiceImpl(WorkflowApplyLineUserService workflowApplyLineUserService) {
        this.workflowApplyLineUserService = workflowApplyLineUserService;
    }

    @Override
    public WorkflowApplyLineUserDTO insert(WorkflowApplyLineUserDTO workflowApplyLineUserDTO) {
        try {
            return convert(workflowApplyLineUserService.insert(convert(workflowApplyLineUserDTO)));
        } catch (Exception e) {
            logger.error("WorkflowApplyLineUser create failed.", e);
            throw new RuntimeException("WorkflowApplyLineUser create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(WorkflowApplyLineUserDTO workflowApplyLineUserDTO) {
        try {
            return workflowApplyLineUserService.updateInfoById(convert(workflowApplyLineUserDTO));
        } catch (Exception e) {
            logger.error("WorkflowApplyLineUser update by id failed.", e);
            throw new RuntimeException("WorkflowApplyLineUser update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(WorkflowApplyLineUserDTOQuery workflowApplyLineUserDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowApplyLineUserDTOQuery.getW_id() != null && workflowApplyLineUserDTOQuery.getW_id() > 0) {
                query.put("w_id", workflowApplyLineUserDTOQuery.getW_id());
            }

            if (workflowApplyLineUserDTOQuery.getIds() != null && !workflowApplyLineUserDTOQuery.getIds().equals("")) {
                query.put("ids", workflowApplyLineUserDTOQuery.getIds());
            }

            if (workflowApplyLineUserDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", workflowApplyLineUserDTOQuery.getDeleteFlag());
            }

            return workflowApplyLineUserService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("WorkflowApplyLineUser update by query failed.", e);
            throw new RuntimeException("WorkflowApplyLineUser update by query failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowApplyLineUserDTO selectById(Integer id) {
        try {
            return convert(workflowApplyLineUserService.selectById(id));
        } catch (Exception e) {
            logger.error("WorkflowApplyLineUser select by id failed.", e);
            throw new RuntimeException("WorkflowApplyLineUser select by id failed, Please order your code.", e);
        }
    }

    @Override
    public WorkflowApplyLineUserDTO selectObjByQuery(WorkflowApplyLineUserDTOQuery workflowApplyLineUserDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (workflowApplyLineUserDTOQuery.getApplyId() != null) {
                query.put("applyId", workflowApplyLineUserDTOQuery.getApplyId());
            }

            if (workflowApplyLineUserDTOQuery.getLineId() != null) {
                query.put("lineId", workflowApplyLineUserDTOQuery.getLineId());
            }

            if (workflowApplyLineUserDTOQuery.getPlayerType() != null) {
                query.put("playerType", workflowApplyLineUserDTOQuery.getPlayerType());
            }

            if (workflowApplyLineUserDTOQuery.getApproverType() != null) {
                query.put("approverType", workflowApplyLineUserDTOQuery.getApproverType());
            }

            return convert(workflowApplyLineUserService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("WorkflowApplyLineUser select obj by query failed.", e);
            throw new RuntimeException("WorkflowApplyLineUser select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<WorkflowApplyLineUserDTO> getListForPage(WorkflowApplyLineUserDTOQuery workflowApplyLineUserDTOQuery) {
        Query query = Query.page(workflowApplyLineUserDTOQuery.getPn(), workflowApplyLineUserDTOQuery.getPageSize());

        if (workflowApplyLineUserDTOQuery.getId() != null && workflowApplyLineUserDTOQuery.getId() > 0) {
            query.put("id", workflowApplyLineUserDTOQuery.getId());
        }

        if (workflowApplyLineUserDTOQuery.getIds() != null && !workflowApplyLineUserDTOQuery.getIds().equals("")) {
            query.put("ids", workflowApplyLineUserDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(workflowApplyLineUserDTOQuery.getLikeName())) {
            query.put("likeName", workflowApplyLineUserDTOQuery.getLikeName());
        }

        if (workflowApplyLineUserDTOQuery.getLineId() != null) {
            query.put("lineId", workflowApplyLineUserDTOQuery.getLineId());
        }

        if (StringUtils.isNotBlank(workflowApplyLineUserDTOQuery.getShowDate())) {
            query.put("showDate", workflowApplyLineUserDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(workflowApplyLineUserDTOQuery.getStartDate())) {
            query.put("startDate", workflowApplyLineUserDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(workflowApplyLineUserDTOQuery.getEndDate())) {
            query.put("endDate", workflowApplyLineUserDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(workflowApplyLineUserDTOQuery.getOrderBy())) {
            query.put("orderBy", workflowApplyLineUserDTOQuery.getOrderBy());
        }

        List<WorkflowApplyLineUserDTO> list = convert(workflowApplyLineUserService.selectByQuery(query));
        Page<WorkflowApplyLineUserDTO> page = Page.from(workflowApplyLineUserDTOQuery.getPn(), workflowApplyLineUserDTOQuery.getPageSize(), list);
        page.setTotalItems(workflowApplyLineUserService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return workflowApplyLineUserService.deleteById(id);
        } catch (Exception e) {
            logger.error("WorkflowApplyLineUser delete failed.", e);
            throw new RuntimeException("WorkflowApplyLineUser delete failed, Please order your code.", e);
        }
    }
}